package com.servlet;

import com.entity.Blog;
import com.service.BlogService;
import com.service.BlogServiceImpl;
import com.util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "BlogServlet",urlPatterns = {"/BlogServlet"})
public class BlogServlet extends HttpServlet {

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//1、获取用户请求==》参数==》条件

		String currentPage = request.getParameter("currentPage");

		PageUtil<Blog> pageUtil = new PageUtil<Blog>();
		if (currentPage!=null){
			pageUtil.setCurrentPage(Integer.valueOf(currentPage)); //将用户给的页码，作用数据查询的当前
		}
		//2、操作
		BlogService blogService = new BlogServiceImpl();
		blogService.findByPage(pageUtil);

//		List<Blog> list = blogService.findByPage(pageUtil); //分页的数据
//		int total = blogService.findPageCount();//总行数
//		pageUtil.setTotalSize(total);
//		pageUtil.setShowList(list);

		//3、页面跳转==>传递数据==》转发
		//request.setAttribute("list", list);
		request.setAttribute("pageUtil", pageUtil);
		request.getRequestDispatcher("index.jsp").forward(request, response);
	}
}
